/*
 * Created by JFormDesigner on Thu Jun 27 12:18:46 CST 2024
 */

package scct.zhurui.ui;

import scct.zhurui.dao.UserDao;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

/**
 * @author zr155
 */
public class DIgUseradd extends JDialog {
    public DIgUseradd(Window owner) {
        super(owner);
        initComponents();
    }


    private void radioButton1ComponentMoved(ComponentEvent e) {
        // TODO add your code here
    }

    private void btnAdd(ActionEvent e) {
        String username = txtName.getText();
        String pass = txtPass.getText();
        int age = Integer.parseInt(txtAge.getModel().getValue().toString());
        String gender = radMail.isSelected() ? "男" : "女";
        String mobile = textField11.getText();
        if (!username.matches("^\\w{3,8}")) {
            JOptionPane.showMessageDialog(this, "请输入合法的用户名");
            return;
        }
        if (!pass.matches("^\\w{3,8}")) {
            JOptionPane.showMessageDialog(this, "请输入合法的密码");
            return;
        }
        if (age < 10 || age > 100) {
            JOptionPane.showMessageDialog(this, "请输入正确的年龄");
            return;
        }
        if (!mobile.matches("^1[3-9]\\d{9}$")) {
            JOptionPane.showMessageDialog(this, "请输入合法的手机号码");
            return;
        }
        UserDao userDao = new UserDao();
        if (userDao.isDupname(username)){
            JOptionPane.showMessageDialog(this,"您输入的用户名已存在");
            return;
        }
        if (userDao.addUser(username, pass, gender, age, mobile)) {
            JOptionPane.showMessageDialog(this, "添加成功");
        }
        this.dispose();
    }
    // TODO add your code here


    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents  @formatter:off
        // Generated using JFormDesigner Evaluation license - zhur
        label1 = new JLabel();
        label2 = new JLabel();
        label3 = new JLabel();
        label5 = new JLabel();
        label6 = new JLabel();
        txtName = new JTextField();
        txtPass = new JTextField();
        textField11 = new JTextField();
        txtAge = new JSpinner();
        radMail = new JRadioButton();
        radFemale = new JRadioButton();
        btnAdd = new JButton();
        btnCancel = new JButton();

        //======== this ========
        setTitle("\u6dfb\u52a0\u7528\u6237");
        addComponentListener(new ComponentAdapter() {
            @Override
            public void componentMoved(ComponentEvent e) {
                radioButton1ComponentMoved(e);
            }
        });
        Container contentPane = getContentPane();
        contentPane.setLayout(null);

        //---- label1 ----
        label1.setText("\u7528\u6237\u540d");
        contentPane.add(label1);
        label1.setBounds(new Rectangle(new Point(95, 29), label1.getPreferredSize()));

        //---- label2 ----
        label2.setText("\u5bc6\u7801");
        contentPane.add(label2);
        label2.setBounds(105, 66, 35, 17);

        //---- label3 ----
        label3.setText("\u624b\u673a\u53f7");
        contentPane.add(label3);
        label3.setBounds(95, 102, label3.getPreferredSize().width, 17);

        //---- label5 ----
        label5.setText("\u5e74\u9f84");
        contentPane.add(label5);
        label5.setBounds(105, 138, label5.getPreferredSize().width, 17);

        //---- label6 ----
        label6.setText("\u6027\u522b");
        contentPane.add(label6);
        label6.setBounds(105, 165, 33, 17);
        contentPane.add(txtName);
        txtName.setBounds(145, 25, 145, 25);
        contentPane.add(txtPass);
        txtPass.setBounds(145, 62, 145, 25);
        contentPane.add(textField11);
        textField11.setBounds(145, 98, 145, 25);

        //---- txtAge ----
        txtAge.setModel(new SpinnerNumberModel((byte) 0, (byte) 0, (byte) 127, (byte) 1));
        contentPane.add(txtAge);
        txtAge.setBounds(145, 134, 145, 25);

        //---- radMail ----
        radMail.setText("\u7537");
        radMail.setSelected(true);
        radMail.addComponentListener(new ComponentAdapter() {
            @Override
            public void componentMoved(ComponentEvent e) {
                radioButton1ComponentMoved(e);
            }
        });
        contentPane.add(radMail);
        radMail.setBounds(160, 165, 40, 16);

        //---- radFemale ----
        radFemale.setText("\u5973");
        radFemale.addComponentListener(new ComponentAdapter() {
            @Override
            public void componentMoved(ComponentEvent e) {
                radioButton1ComponentMoved(e);
            }
        });
        contentPane.add(radFemale);
        radFemale.setBounds(220, 165, 40, 16);

        //---- btnAdd ----
        btnAdd.setText("\u6dfb\u52a0");
        btnAdd.addActionListener(e -> btnAdd(e));
        contentPane.add(btnAdd);
        btnAdd.setBounds(new Rectangle(new Point(95, 205), btnAdd.getPreferredSize()));

        //---- btnCancel ----
        btnCancel.setText("\u53d6\u6d88");
        contentPane.add(btnCancel);
        btnCancel.setBounds(new Rectangle(new Point(220, 205), btnCancel.getPreferredSize()));

        {
            // compute preferred size
            Dimension preferredSize = new Dimension();
            for(int i = 0; i < contentPane.getComponentCount(); i++) {
                Rectangle bounds = contentPane.getComponent(i).getBounds();
                preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
            }
            Insets insets = contentPane.getInsets();
            preferredSize.width += insets.right;
            preferredSize.height += insets.bottom;
            contentPane.setMinimumSize(preferredSize);
            contentPane.setPreferredSize(preferredSize);
        }
        setSize(400, 300);
        setLocationRelativeTo(getOwner());

        //---- buttonGroup1 ----
        ButtonGroup buttonGroup1 = new ButtonGroup();
        buttonGroup1.add(radMail);
        buttonGroup1.add(radFemale);
        // JFormDesigner - End of component initialization  //GEN-END:initComponents  @formatter:on
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables  @formatter:off
    // Generated using JFormDesigner Evaluation license - zhur
    private JLabel label1;
    private JLabel label2;
    private JLabel label3;
    private JLabel label5;
    private JLabel label6;
    private JTextField txtName;
    private JTextField txtPass;
    private JTextField textField11;
    private JSpinner txtAge;
    private JRadioButton radMail;
    private JRadioButton radFemale;
    private JButton btnAdd;
    private JButton btnCancel;
    /* JFormDesigner - End of variables declaration  //GEN-END:variables  @formatter:on */

}